package controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"net/http"
	"teresa/service"
)

var Servicev1 servicev1

type servicev1 struct{}

func (s *servicev1) GetServices(ctx *gin.Context) {
	params := new(struct {
		Namespace    string `form:"namespace" binding:"required"`
		FilterName string `form:"filtername"`
		Limit        int    `form:"limit"`
		Page         int    `form:"page"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("参数绑定失败", err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败" + err.Error(),
			"data": nil,
		})
		return
	}

	data, err := service.Servicev1.GetServices(params.Namespace, params.FilterName, params.Limit, params.Page)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取service列表成功",
		"data": data,
	})
}

func (s *servicev1) GetServiceDetail(ctx *gin.Context) {
	params := new(struct {
		Namespace    string `form:"namespace"`
		ServicesName string `form:"servicename"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("bind绑定参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败",
			"data": nil,
		})
		return
	}
	data, err := service.Servicev1.GetServiceDetail(params.ServicesName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var JsonServiceData Jsonservicedata
	jsonservicedata, err := json.Marshal(data)
	if err != nil {
		fmt.Println("数据转换失败",err.Error())
		return
	}
	err = json.Unmarshal(jsonservicedata, &JsonServiceData)
	if err != nil {
		fmt.Println("数据加载失败",err.Error())
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取service详情成功",
		"data": JsonServiceData,
	})
}

func (s *servicev1) CreateService(ctx *gin.Context) {
	var (
		servicesCreate = new(service.ServiceCreate)
		err   error
	)
	if err = ctx.ShouldBindJSON(servicesCreate); err != nil {
		logger.Error("bind请求参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	err = service.Servicev1.CreateService(servicesCreate)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建service成功",
		"data": nil,
	})
}

func (s *servicev1) UpdateService(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	err := service.Servicev1.UpdateService(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新service成功",
		"data": nil,
	})
}

func (s *servicev1) DeleteService(ctx *gin.Context) {
	params := new(struct {
		Namespace   string `form:"namespace"`
		ServiceName string `form:"servicename"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数绑定失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "Bind请求参数绑定失败," + err.Error(),
			"data": nil,
		})
		return
	}
	err := service.Servicev1.DeleteService(params.Namespace, params.ServiceName)
	if err != nil {
		logger.Error("删除service失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "删除service失败" + err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除service成功",
		"data": nil,
	})
}
